// pages/mine/rank/rank.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../../api/index'),
    utils = require('../../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 1,
        rank: false, // 按销售排行 true 按销售额排行 false
        status: 0,
        toast: {
            visible: false,
            text: ''
        },
        filter: {
            top: 10,
            timeType: 1, // 1 昨日 2 上周 3 上月
        },
        userInfo: {},
        list: [],
        imageBaseUri: globalData.imageBaseUri
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            userInfo: wx.getStorageSync('userinfo') || {}
        })
        this.reload()
    },

    reload() {
        this.setData({
            status: 0
        })
        this.getRank();
        this.getBenefits();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    switchTab(e) {
        let tab = e.currentTarget.dataset.tab;
        this.setData({
            active: tab,
            "filter.timeType": tab
        })
        this.getRank()

    },

    switchRank() {
        this.setData({
            rank: !this.data.rank
        })
    },

    // 获取排行列表
    async getRank() {
        wx.showLoading({
          title: '加载中',
        })
        try {
            let res = await api.balance.getRankList(this.data.filter),
                data = res.data.ResData || [];
            console.log(res, '排行列表')
            this.setData({
                status: 2,
                list: data,
                rankList: data.splice(3,10),
                pageData: res.data || {}
            })
            console.log(this.data.rankList,'ranklist')
        } catch (e) {
            console.log(e, '获取排行列表失败')
            this.setData({
                status: 1
            })
        } finally {
            wx.hideLoading()
        }
    },

    // 获取我的收益
	async getBenefits() {
		try {
			let res = await api.balance.getMyBenefits(),
				data = res.data.ResData || {};
			this.setData({
				status: 2,
				total: data
			})
			console.log(res, '我的收益')
		} catch (e) {
			console.log(e, '获取我的收益失败')
		}
	},

    
})